<?php

/**
 * User: htt
 * Date: 2019-02-20
 * Time: 上午10:54
 */

namespace App\Models\User;

use App\Models\Admin\UserAccessModel;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Cookie;
//class UserModel extends Authenticatable implements JWTSubject
class UserModel extends Authenticatable {

    use Notifiable;

    protected $table = 'users';
    protected $fillable = ['phone', 'password', 'username'];
    protected $hidden = ['password', 'remember_token'];
    protected $primaryKey = 'id';
    public $timestamps = false;

    public function getJWTIdentifier() {
        return $this->getKey();
    }

    public function getJWTCustomClaims() {
        return [];
    }

    public function getCreateReloadNeedParams() {
        return [
            'phone', 'password', 'name', 'type'
        ];
    }

    const defaultHead = '/h5/images/toux01.png';

    /**
     *  获取用户信息
     */
    public static function getinfo($id) {
        $one = self::where('id', $id)->first();
        if ($one) {
            $one['head'] = \App\Jobs\Fun::pImg($one['head'] ? $one['head'] : self::defaultHead);
            return $one;
        } else {
            return [
                'head' => self::defaultHead,
                'name' => '未知用户',
                'id'=>null
            ];
        }
    }
    /**
     * 如果是游客用户,直接返回true
    */
    public function isGuest(){

        return !$this->getId();
    }

    private $_model = null;
    /**
     * @return type UserModel
     */
    public function getModel($token = null) {
        if ($this->_model) {
            return $this->_model;
        }
          //  exit('sssssss');
        if (!$token) {
            if (isset($_POST['token'])) {
                $token = $_POST['token'];
            } elseif (isset($_GET['token'])) {
                $token = $_GET['token'];
            }else{
               $token=$_COOKIE['token']??'';
            }
        }
     
        
        $modelAcc = UserAccessModel::where('token', $token)
                ->select('uid')
                ->orderBy('id', 'desc')
                ->first();
        if(!$modelAcc){
            return null;
        }
        $uid=$modelAcc->uid;
        $model= UserModel::where('id',$uid) 
                ->select('id','name','money','type','head')
                ->first();
        
        
        if (!$model) {
            return null;
        }
        $this->_model = $model;
        return $this->_model;
    }

    public function getId($token = null) {
        $model = $this->getModel($token);
        if ($model) {
            return $model->id;
        } else {
            return null;
        }
    }

    public function getMoney($token = null) {
        $model = $this->getModel($token);
        if ($model&&$model->money) {
            return (float)$model->money;
        } else {
            return 0;
        }
    }
    
    /***
     * 获取指定id的用户名
*/
    public static function idToName($id){
         $one = self::where('id', $id)->select('name')->first();
         if($one){
             return $one->name;
         }else{
             return '未知';
         }
    }
     
    public function getHead(){
        if($this->head){
            return \App\Jobs\Fun::pImg($this->head);
        }else{
            return self::defaultHead;
        }
    }
    
    /**
     * 返回类型名称
     */
    public function getTypeName(){
        $UserRe=new \App\Repositories\Admin\UserRepository;
        $model=$this->getModel();
        return $UserRe->getTypeName($model);
    }
    
    

}
